package com.refusea.train.mapper

import com.refusea.train.domain.entity.RichText
import com.refusea.train.domain.entity.RichText.Cat
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Options
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

/**
 * @author Zhang Yan 2024/3/30
 **/
@Mapper
interface RichTextMapper {

    @Insert(
        """
        insert into rich_text 
        (school_id, cat, entity_id, content) 
        values 
        (#{schoolId}, #{cat}, #{entityId}, #{content})
        """
    )
    @Options(useGeneratedKeys = true, keyProperty = "id")
    fun insert(richText: RichText): Int

    @Select("select * from rich_text where school_id=#{schoolId} and cat=#{cat} and entity_id=#{entityId}")
    fun query(richText: RichText): RichText?

    @Select("select * from rich_text where id=#{id}")
    fun select(id: Int): RichText?

    @Select(
        """
        select * from rich_text 
        where school_id=#{schoolId} and cat=#{cat} and entity_id=#{entityId}
        """
    )
    fun select3(
        @Param("schoolId") schoolId: Int,
        @Param("cat") cat: Cat,
        @Param("entityId") entityId: Int,
    ): RichText?

    @Update("update rich_text set content=#{content} where id=#{id}")
    fun update(richText: RichText)

    @Select(
        """
        select content from rich_text 
        where school_id=#{schoolId} and cat=#{cat} and entity_id=#{entityId}
        """
    )
    fun selectContent(
        @Param("schoolId") schoolId: Int,
        @Param("cat") cat: Cat,
        @Param("entityId") entityId: Int,
    ): String?

    @Select("select * from rich_text")
    fun listAll(): List<RichText>
}
